
package cn.hp.controller;

import cn.hp.entity.Announcement;
import cn.hp.entity.User;
import cn.hp.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hp.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * post 新增
 * put 修改
 * delete 删除
 * get 查询
 */

@RestController
@RequestMapping("/api/announcement")
@Api(value = "/announcement", tags = "公告管理")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询",notes = "分页查询所有公告")
    public Result page(Announcement announcement,@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        return Result.success(announcementService.selectAnnouncements(announcement,page, limit));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "删除公告")
    public Result del(@PathVariable Integer id) {
        System.out.println("id+" + id);
        int i = announcementService.deleteAnnouncementById(id);
        if (i == 1) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }

    }

    @PostMapping("/a1")
    @ApiOperation(value = "新增",notes = "新增公告")
    public Result insert(@RequestBody Announcement announcement) {
        int i = announcementService.insertAnnouncement(announcement);
        if (i == 1) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }

    }

    @PutMapping("/a1")
    @ApiOperation(value = "修改",notes = "修改公告")
    public Result update(@RequestBody Announcement announcement) {
        int i = announcementService.updateAnnouncement(announcement);
        if (i == 1) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }
    @GetMapping
    @ApiOperation(value = "查询", notes = "查询所有公告")
    public Result find(Announcement announcement){
        List<Announcement> list = announcementService.selectAnnouncements(announcement);
        return Result.success(list);
    }
}
